package com.lora.communication.mqtt;

import com.lora.service.EquipmentService;
import com.lora.util.ApplicationUtil;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据处理类
 */
public class LoraMqttHandler {
    Logger logger= LoggerFactory.getLogger(LoraMqttHandler.class);
    public void dealWith(String topic, MqttMessage mqttMessage){
        logger.info("接收到主题为{}的数据{}", topic, mqttMessage.toString());
        String suffix = topic.substring(topic.lastIndexOf("/")+1);
        if(suffix.equals("rx")){
            EquipmentService equipmentService= ApplicationUtil.getBean("equipmentService");
            topic = topic.substring(0, topic.lastIndexOf("/"));
            String equipmentNumber = topic.substring(topic.lastIndexOf("/")+1);
            try {
                equipmentService.process(equipmentNumber,mqttMessage.toString());
            } catch (Exception e) {
                logger.error(e.getLocalizedMessage());
            }
        }
    }
}
